from datetime import datetime
from datetime import date

def days_between(d1, d2):
    return abs((d2 - d1).days)


class Room:
    """ This class represents a room like living room, bedroom, etc """
    def __init__(self, n):
        self._name = n
        self._level = "normal"
        self._itemList = list()

    def addItem(self, i):
        self._itemList.append(i)

    def hasItem(self, i_n):
        for i in self._itemList:
            if i._name == i_n:
                return True
        return False

    def removeItemByName(self, i_n):
        i = 0
        del_index = -1
        while i < len(self._itemList):
            if self._itemList[i]._name == i_n:
                del_index = i
            i += 1
        if del_index != -1:
            del self._itemList[del_index]

    def initDate(self):
        if self._itemList is not None:
            for item in self._itemList:
                item.initDate()
        else:
            print("No item available")

    def calculateCleanLevel(self):
        somme = 0
        for item in self._itemList:
            somme += days_between(item._lastCleanedDate, date.today())

        if len(self._itemList) != 0:
            somme = somme / len(self._itemList)

        if somme > 15:
            self._level = "terrible"
        elif somme > 10:
            self._level = "very dirty"
        elif somme > 5:
            self._level = "dirty"
        elif somme > 3:
            self._level = "not clean"
        else:
            self._level = "clean"

class Item:
    """ This class reprsents an item in the room, like a table or the window """
    def __init__(self, n):
        self._name = n
        self._lastCleanedDate = date.today()

    def initDate(self):
        self._lastCleanedDate = date.today()


class CleanSystem:
    def __init__(self):
        self._roomList = list()

    def getRoomByName(self, rname):
        for room in self._roomList:
            if room._name == rname:
                return room
        return

    def readListFromFile(self, file_path):
        with open(file_path, 'r') as f:
            for line in f:
                line_list = line.split('\t')
                if len(line_list) is not 0 and line_list[0] is 'r':
                    room_obj = Room(line_list[1][:-1])
                    self._roomList.append(room_obj)
                elif len(line_list) is not 0 and line_list[0] is 'i':
                    if len(line_list) <= 3:
                        item_obj = Item(line_list[2][:-1])
                        item_obj.initDate()
                        if self.getRoomByName(line_list[1]) is not None:       
                            self.getRoomByName(line_list[1]).addItem(item_obj)
                    else:
                        item_obj = Item(line_list[2])
                        item_obj._lastCleanedDate = (datetime.strptime(line_list[3][:-1], '%Y-%m-%d')).date()
                        
                        if self.getRoomByName(line_list[1]) is not None:       
                            self.getRoomByName(line_list[1]).addItem(item_obj)
                        

    def saveListInFile(self, file_path):
        with open(file_path, 'w') as f:
            for room in self._roomList:
                roomstring = "r\t" + room._name+"\n"
                f.write(roomstring)
                for item in room._itemList:
                    itemstring = "i\t" + room._name + "\t" + item._name +"\t" + str(item._lastCleanedDate) + "\n"
                    f.write(itemstring)

    def roomlist_to_string(self):
        output_text=""
        for room in self._roomList:
            output_text += room._name + "          " + room._level + "\n"
            for item in room._itemList:
                if item._lastCleanedDate is "":   
                    output_text += "\t" + item._name + "\n"
                else:
                    output_text += "\t" + item._name + "\t" + str(days_between(item._lastCleanedDate, date.today())) + "\n"
        return output_text

    def updateStatus(self, r_name, i_name):
        find = 0 # room not find
        for room in self._roomList:
            if room._name == r_name:
                find = 1 # room found
                for item in room._itemList:
                    if item._name == i_name:
                        find = 2 # item found
                        item._lastCleanedDate = date.today()
        return find

    def hasRoom(self, n):
        for room in self._roomList:
            if room._name == n:
                return True
        return False

    def hasItemOfRoom(self, room_n, item_n):
        # we suppose the room exists
        r = self.getRoomByName(room_n)
        if r.hasItem(item_n):
            return True
        else:
            return False
        

    def addRoom(self, r):
        self._roomList.append(r)

    def removeRoom(self, room_n):
        i = 0
        del_index = -1
        while i < len(self._roomList):
            if self._roomList[i]._name == room_n:
                del_index = i
            i += 1
        if del_index != -1:
            del self._roomList[del_index]

    def calculateCleanLevel(self):
        for room in self._roomList:
            room.calculateCleanLevel()
